<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="深山磨柴斧，一鸣震江湖">
  <link rel="icon" href="/img/ic_head.jpg">
  <title>Java过渡Kotlin06：扩展</title>
  
  
  <meta property="og:title" content="Java过渡Kotlin06：扩展">
  
  
  <meta property="og:url" content="http://example.com/2020/03/02/Java%E8%BF%87%E6%B8%A1Kotlin06%EF%BC%9A%E6%89%A9%E5%B1%95/index.html">
  
  
  <meta property="og:img" content="/img/ic_head.jpg">
  
  
  <meta property="og:img" content="深山磨柴斧，一鸣震江湖">
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2020-03-02">
  <meta property="og:article:modified_time" content="2021-04-14">
  <meta property="og:article:author" content="Cupster">
  
  
  <meta property="og:article:tag" content="Kotlin">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/ic_head.jpg" alt="logo">
      
      <span class="navbar-logo-dsc">不负明天</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      Java过渡Kotlin06：扩展
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2020-03-02T13:00:01.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2020-03-02</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/Kotlin/" class="post-meta-link">Kotlin</a>
    
    
    
    <span class="dot"></span>
    <span>1.8k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/Kotlin/" class="post-meta-link">Kotlin</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <h1 id="1-是什么"><a href="#1-是什么" class="headerlink" title="1.是什么"></a>1.是什么</h1><p>Kotlin 可以对一个类的属性和方法进行扩展，且不需要继承或使用 Decorator 模式。</p>
<p>扩展是一种静态行为，对被扩展的类代码本身不会造成任何影响。</p>
<p>上代码：</p>
<pre><code>class User(var name:String)

/**扩展函数**/
fun User.Print()&#123;
    print(&quot;用户名 $name&quot;)
&#125;

fun main(arg:Array&lt;String&gt;)&#123;
    var user = User(&quot;Runoob&quot;)
    user.Print()
&#125;
</code></pre>
<p>实践</p>
<pre><code>// 扩展函数 swap,调换不同位置的值
fun MutableList&lt;Int&gt;.swap(index1: Int, index2: Int) &#123;
    val tmp = this[index1]     //  this 对应该列表
    this[index1] = this[index2]
    this[index2] = tmp
&#125;

fun main(args: Array&lt;String&gt;) &#123;

    val l = mutableListOf(1, 2, 3)
    // 位置 0 和 2 的值做了互换
    l.swap(0, 2) // &#39;swap()&#39; 函数内的 &#39;this&#39; 将指向 &#39;l&#39; 的值

    println(l.toString())
&#125;
</code></pre>
<p><strong>注意</strong></p>
<p><strong>注意</strong></p>
<p><strong>注意</strong></p>
<p>扩展函数是静态解析的，并不是接收者类型的虚拟成员，在调用扩展函数时，具体被调用的的是哪一个函数，由调用函数的的对象表达式来决定的，而不是动态的类型决定的:</p>
<pre><code>open class C

class D: C()

fun C.foo() = &quot;c&quot;   // 扩展函数 foo

fun D.foo() = &quot;d&quot;   // 扩展函数 foo

fun printFoo(c: C) &#123;
    println(c.foo())  // 类型是 C 类
&#125;

fun main(arg:Array&lt;String&gt;)&#123;
    printFoo(D())//结果是C
&#125;
</code></pre>
<p><strong>注意</strong></p>
<p><strong>注意</strong></p>
<p><strong>注意</strong></p>
<p>扩展函数和成员函数一致，则使用该函数时，会优先使用成员函数。</p>
<pre><code>class C &#123;
    fun foo() &#123; println(&quot;成员函数&quot;) &#125;
&#125;

fun C.foo() &#123; println(&quot;扩展函数&quot;) &#125;

fun main(arg:Array&lt;String&gt;)&#123;
    var c = C()
    c.foo()
&#125;
</code></pre>
<h1 id="2-扩展一个空对象"><a href="#2-扩展一个空对象" class="headerlink" title="2.扩展一个空对象"></a>2.扩展一个空对象</h1><pre><code>fun Any?.toString(): String &#123;
    if (this == null) return &quot;null&quot;
    // 空检测之后，“this”会自动转换为非空类型，所以下面的 toString()
    // 解析为 Any 类的成员函数
    return toString()
&#125;
fun main(arg:Array&lt;String&gt;)&#123;
    var t = null
    println(t.toString())//输出  null
&#125;
</code></pre>
<p>扩展属性只能被声明为 val。</p>
<p>扩展属性只能被声明为 val。</p>
<p>扩展属性只能被声明为 val。</p>
<h1 id="3-伴生对象的扩展"><a href="#3-伴生对象的扩展" class="headerlink" title="3.伴生对象的扩展"></a>3.伴生对象的扩展</h1><pre><code>class MyClass &#123;
    companion object &#123; &#125;  // 将被称为 &quot;Companion&quot;
&#125;

fun MyClass.Companion.foo() &#123;
    println(&quot;伴随对象的扩展函数&quot;)
&#125;

val MyClass.Companion.no: Int
    get() = 10

fun main(args: Array&lt;String&gt;) &#123;
    println(&quot;no:$&#123;MyClass.no&#125;&quot;)
    MyClass.foo()
&#125;
</code></pre>
<p>结果 </p>
<pre><code>no:10
伴随对象的扩展函数
</code></pre>
<h1 id="4-扩展的作用域"><a href="#4-扩展的作用域" class="headerlink" title="4.扩展的作用域"></a>4.扩展的作用域</h1><p>通常扩展函数或属性定义在顶级包下:</p>
<pre><code>package foo.bar

fun Baz.goo() &#123; …… &#125; 
</code></pre>
<p>要使用所定义包之外的一个扩展, 通过import导入扩展的函数名进行使用:</p>
<pre><code>package com.example.usage

import foo.bar.goo // 导入所有名为 goo 的扩展
                   // 或者
import foo.bar.*   // 从 foo.bar 导入一切

fun usage(baz: Baz) &#123;
    baz.goo()
&#125;
</code></pre>
<h1 id="5-扩展声明为成员"><a href="#5-扩展声明为成员" class="headerlink" title="5. 扩展声明为成员"></a>5. 扩展声明为成员</h1><p>在一个类内部你可以为另一个类声明扩展。</p>
<p>在这个扩展中，有个多个隐含的接受者，其中扩展方法定义所在类的实例称为分发接受者，而扩展方法的目标类型的实例称为扩展接受者。</p>
<pre><code>class D &#123;
    fun bar() &#123; println(&quot;D bar&quot;) &#125;
&#125;

class C &#123;
    fun baz() &#123; println(&quot;C baz&quot;) &#125;

    fun D.foo() &#123;
        bar()   // 调用 D.bar
        baz()   // 调用 C.baz
    &#125;

    fun caller(d: D) &#123;
        d.foo()   // 调用扩展函数
    &#125;
&#125;

fun main(args: Array&lt;String&gt;) &#123;
    val c: C = C()
    val d: D = D()
    c.caller(d)

&#125;
</code></pre>
<p>实例执行输出结果为：</p>
<pre><code>D bar
C baz
</code></pre>
<p>假如在调用某一个函数，而该函数在分发接受者和扩展接受者均存在，则以扩展接收者优先，要引用分发接收者的成员你可以使用限定的 this 语法</p>
<pre><code>class D &#123;
    fun bar() &#123; println(&quot;D bar&quot;) &#125;
&#125;

class C &#123;
    fun bar() &#123; println(&quot;C bar&quot;) &#125;  // 与 D 类 的 bar 同名

    fun D.foo() &#123;
        bar()         // 调用 D.bar()，扩展接收者优先
        this@C.bar()  // 调用 C.bar()
    &#125;

    fun caller(d: D) &#123;
        d.foo()   // 调用扩展函数
    &#125;
&#125;

fun main(args: Array&lt;String&gt;) &#123;
    val c: C = C()
    val d: D = D()
    c.caller(d)

&#125;
</code></pre>
<p>实例执行输出结果为：</p>
<pre><code>D bar
C bar
</code></pre>
<p>以成员的形式定义的扩展函数, 可以声明为 open , 而且可以在子类中覆盖. 也就是说, 在这类扩展函数的派 发过程中, 针对分发接受者是虚拟的(virtual), 但针对扩展接受者仍然是静态的</p>
<pre><code>open class D &#123;
&#125;

class D1 : D() &#123;
&#125;

open class C &#123;
    open fun D.foo() &#123;
        println(&quot;D.foo in C&quot;)
    &#125;

    open fun D1.foo() &#123;
        println(&quot;D1.foo in C&quot;)
    &#125;

    fun caller(d: D) &#123;
        d.foo()   // 调用扩展函数
    &#125;
&#125;

class C1 : C() &#123;
    override fun D.foo() &#123;
        println(&quot;D.foo in C1&quot;)
    &#125;

    override fun D1.foo() &#123;
        println(&quot;D1.foo in C1&quot;)
    &#125;
&#125;


fun main(args: Array&lt;String&gt;) &#123;
    C().caller(D())   // 输出 &quot;D.foo in C&quot;
    C1().caller(D())  // 输出 &quot;D.foo in C1&quot; —— 分发接收者虚拟解析
    C().caller(D1())  // 输出 &quot;D.foo in C&quot; —— 扩展接收者静态解析

&#125;
</code></pre>
<p>实例执行输出结果为：</p>
<pre><code>D.foo in C
D.foo in C1
D.foo in C
</code></pre>
<p>伴生对象内的成员相当于 Java 中的静态成员，其生命周期伴随类始终，在伴生对象内部可以定义变量和函数，这些变量和函数可以直接用类名引用。</p>
<p>对于伴生对象扩展函数，有两种形式，一种是在类内扩展，一种是在类外扩展，这两种形式扩展后的函数互不影响（甚至名称都可以相同），即使名称相同，它们也完全是两个不同的函数，并且有以下特点：</p>
<p> （1）类内扩展的伴随对象函数和类外扩展的伴随对象可以同名，它们是两个独立的函数，互不影响；<br> （2）当类内扩展的伴随对象函数和类外扩展的伴随对象同名时，类内的其它函数优先引用类内扩展的伴随对象函数，即对于类内其它成员函数来说，类内扩展屏蔽类外扩展；<br> （3）类内扩展的伴随对象函数只能被类内的函数引用，不能被类外的函数和伴随对象内的函数引用；<br> （4）类外扩展的伴随对象函数可以被伴随对象内的函数引用，；<br>例如以下代码</p>
<pre><code>class MyClass &#123;
    companion object &#123;
        val myClassField1: Int = 1
        var myClassField2 = &quot;this is myClassField2&quot;
        fun companionFun1() &#123;
            println(&quot;this is 1st companion function.&quot;)
            foo()
        &#125;
        fun companionFun2() &#123;
            println(&quot;this is 2st companion function.&quot;)
            companionFun1()
        &#125;
    &#125;
    fun MyClass.Companion.foo() &#123;
        println(&quot;伴随对象的扩展函数（内部）&quot;)
    &#125;
    fun test2() &#123;
        MyClass.foo()
    &#125;
    init &#123;
        test2()
    &#125;
&#125;
val MyClass.Companion.no: Int
    get() = 10
fun MyClass.Companion.foo() &#123;
    println(&quot;foo 伴随对象外部扩展函数&quot;)
&#125;
fun main(args: Array&lt;String&gt;) &#123;
    println(&quot;no:$&#123;MyClass.no&#125;&quot;)
    println(&quot;field1:$&#123;MyClass.myClassField1&#125;&quot;)
    println(&quot;field2:$&#123;MyClass.myClassField2&#125;&quot;)
    MyClass.foo()
    MyClass.companionFun2()
&#125;
</code></pre>
<p>运行结果：</p>
<pre><code>no:10
field1:1
field2:this is myClassField2
foo 伴随对象外部扩展函数
this is 2st companion function.
this is 1st companion function.
foo 伴随对象外部扩展函数
</code></pre>

  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/about">Cupster</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/2020/03/02/Java%E8%BF%87%E6%B8%A1Kotlin06%EF%BC%9A%E6%89%A9%E5%B1%95/">http://example.com/2020/03/02/Java%E8%BF%87%E6%B8%A1Kotlin06%EF%BC%9A%E6%89%A9%E5%B1%95/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2020/03/03/Java过渡Kotlin07：数据类&amp;密封类/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">Java过渡Kotlin07：数据类&amp;密封类 </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2020/03/02/Java过渡Kotlin05：继承和接口/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">Java过渡Kotlin05：继承和接口 </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-text">1.是什么</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E6%89%A9%E5%B1%95%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%AF%B9%E8%B1%A1"><span class="toc-text">2.扩展一个空对象</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E4%BC%B4%E7%94%9F%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%89%A9%E5%B1%95"><span class="toc-text">3.伴生对象的扩展</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E6%89%A9%E5%B1%95%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-text">4.扩展的作用域</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E6%89%A9%E5%B1%95%E5%A3%B0%E6%98%8E%E4%B8%BA%E6%88%90%E5%91%98"><span class="toc-text">5. 扩展声明为成员</span></a></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/ic_head.jpg" class="author-img">

<p class="author-name">Cupster</p>
<p class="author-description">深山磨柴斧，一鸣震江湖</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>66</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>31</span>
    <span>标签</span>
  </a>
</div>

<div class="author-card-society">
  
    <div class="author-card-society-icon">
      <a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/">
        <i class="iconfont /img/ic_csdn.png society-icon"></i>
      </a>
    </div>
  
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-text">1.是什么</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E6%89%A9%E5%B1%95%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%AF%B9%E8%B1%A1"><span class="toc-text">2.扩展一个空对象</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E4%BC%B4%E7%94%9F%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%89%A9%E5%B1%95"><span class="toc-text">3.伴生对象的扩展</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E6%89%A9%E5%B1%95%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-text">4.扩展的作用域</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E6%89%A9%E5%B1%95%E5%A3%B0%E6%98%8E%E4%B8%BA%E6%88%90%E5%91%98"><span class="toc-text">5. 扩展声明为成员</span></a></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/Android/">
        <div class="categories-list-item">
          Android
          <span class="categories-list-item-badge">38</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Dev-Base/">
        <div class="categories-list-item">
          Dev-Base
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/Kotlin/">
        <div class="categories-list-item">
          Kotlin
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/自动化脚本/">
        <div class="categories-list-item">
          自动化脚本
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="/tags/Android/" title="Android"><div class="tags-list-item">Android</div></a>
    
    <a href="/tags/Android自定义View/" title="Android自定义View"><div class="tags-list-item">Android自定义View</div></a>
    
    <a href="/tags/MacOS/" title="MacOS"><div class="tags-list-item">MacOS</div></a>
    
    <a href="/tags/Kotlin/" title="Kotlin"><div class="tags-list-item">Kotlin</div></a>
    
    <a href="/tags/Android持久化/" title="Android持久化"><div class="tags-list-item">Android持久化</div></a>
    
    <a href="/tags/CentOS/" title="CentOS"><div class="tags-list-item">CentOS</div></a>
    
    <a href="/tags/Linux/" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="/tags/Android事件/" title="Android事件"><div class="tags-list-item">Android事件</div></a>
    
    <a href="/tags/Android基础/" title="Android基础"><div class="tags-list-item">Android基础</div></a>
    
    <a href="/tags/Android手势/" title="Android手势"><div class="tags-list-item">Android手势</div></a>
    
    <a href="/tags/Scrcpy/" title="Scrcpy"><div class="tags-list-item">Scrcpy</div></a>
    
    <a href="/tags/Android动画/" title="Android动画"><div class="tags-list-item">Android动画</div></a>
    
    <a href="/tags/Git/" title="Git"><div class="tags-list-item">Git</div></a>
    
    <a href="/tags/Java/" title="Java"><div class="tags-list-item">Java</div></a>
    
    <a href="/tags/Docker/" title="Docker"><div class="tags-list-item">Docker</div></a>
    
    <a href="/tags/Android-Dialog/" title="Android Dialog"><div class="tags-list-item">Android Dialog</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-text">1.是什么</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E6%89%A9%E5%B1%95%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%AF%B9%E8%B1%A1"><span class="toc-text">2.扩展一个空对象</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E4%BC%B4%E7%94%9F%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%89%A9%E5%B1%95"><span class="toc-text">3.伴生对象的扩展</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E6%89%A9%E5%B1%95%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-text">4.扩展的作用域</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E6%89%A9%E5%B1%95%E5%A3%B0%E6%98%8E%E4%B8%BA%E6%88%90%E5%91%98"><span class="toc-text">5. 扩展声明为成员</span></a></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-02</div>
        <a href="/2021/03/02/Git commit规范/"><div class="recent-posts-item-content">Git commit规范</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-02-16</div>
        <a href="/2021/02/16/AutoJS一文精通AutoJS脚本教程详解/"><div class="recent-posts-item-content">AutoJS一文精通AutoJS脚本教程详解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-21</div>
        <a href="/2021/01/21/架构师进阶：注解Annotation全解/"><div class="recent-posts-item-content">架构师进阶：注解Annotation全解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-06</div>
        <a href="/2021/01/06/ConstraintLayout详解/"><div class="recent-posts-item-content">ConstraintLayout详解</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">不负明天 </a>
      </div>
    </div>

    
    <div class="footer-dsc">
      
      Powered by
      <a href="https://hexo.io/" class="footer-link" target="_blank" rel="nofollow noopener noreferrer">&nbsp;Hexo </a>
      
      
      <span>&nbsp;|&nbsp;</span>
      
      
      Theme -
      <a href="https://github.com/theme-kaze" class="footer-link" target="_blank"
        rel="nofollow noopener noreferrer">&nbsp;Kaze</a>
      
    </div>
    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量686<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数219<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>